package index;

import java.io.File;
import java.util.ArrayList;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;

public class Test {

	/**
	 * @param args
	 * @return 
	 */
//	public static ArrayList<Document> produceDocs() {
//	return null;

//	}

	public static void main(String[] args) {
		//ArrayList<Document> docs = produceDocs();

		try{

			final File INDEX_DIR = new File("index");
			final File docDir = new File("/Users/ale/Documents/workspace/IKM2/rel200");
			IndexWriter writer= new IndexWriter(INDEX_DIR,new StandardAnalyzer(),true);

			buildIndex(writer, docDir);
		} catch (Exception e) {
			System.err.println("Dio");
		}

	}


	static void buildIndex(IndexWriter writer, File docDir) {
		String[] verses = {"Alessandro", "Andrea", "Maria"};
		String[] ages   = {"19", "20", "21"};
		String[] chids   = {"1", "1", "1"};


		Document doc = new Document();

		for (int i = 0 ; i < 3 ; i++) {
			doc.add(new Field("verseContents", verses[i], Store.YES, Field.Index.TOKENIZED));
			doc.add(new Field("age", ages[i], Store.YES, Field.Index.TOKENIZED));
			doc.add(new Field("chid", chids[i], Store.YES, Field.Index.NO));
			//System.out.println(doc.getField("verseContents")+ "\t" + doc.);
		}

		ArrayList<Document> docs = new ArrayList<Document>();
		docs.add(doc);

		//A.L. ==> Document[]
		Document[] Documents = new Document[docs.size()];
		int i=0;
		for(Document docc : docs)
		{
			Documents[i]=docc;
			i++;
		}

		System.out.println(Documents.length); //DBG

		// --> Index
		for(Document d:  Documents)
			try {
				writer.addDocument(d);
				System.out.println(writer.docCount());
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				writer.optimize();
				writer.close();
				System.out.println("Done");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			//return Documents;



	}



}
